var Message = {
    options: {
        appendTo: "body",
        autohide: 5000,//en microsegundos
        interval: 500,
        messageClass: "message",
        messageClassActive: "message-active",
        messageContainerClass: "messageContainer",
        messageWrapperClass: "message-wrapper",
        messageTextClass: "message-text",
        messageTitleClass: "message-title"
    },
    queue: [],
    container: null,
    init: function (a) {
        $.extend(this.options, a || {});
//        if (this.options.autohide) {
//            this.options.autohide *= 1000
//        }
        $(this.options.appendTo).append($('<div id="__messageQueue__container"></div>').addClass(this.options.messageContainerClass));
        this.container = $("div#__messageQueue__container");
        if ("setInterval" in window) {
            window.setInterval(function () {
                Message.check()
            }, this.options.interval)
        } else {
            window.setTimeout(function () {
                Message.check(true)
            }, this.options.interval)
        }
        $("div#__messageQueue__container *").live("selectstart", function () {
            return false
        });
        $("div#__messageQueue__container *").live("mousedown", function () {
            $(this).css("MozUserSelect", "none");
            return false
        })
    },
    check: function (d) {
        var a = new Date().getTime();
        for (var b = 0; b < this.queue.length; b++) {
            var c = this.queue[b];
            if (c.unread) {
                c.unread = false;
                this.show(c.unique)
            } else {
                if (!c.active && !c.sticky && this.options.autohide && ((!c.autohide && c.time < (a - this.options.autohide)) || (c.autohide && c.time < (a - c.autohide)))) {
                    this.hide(c.unique)
                }
            }
        }
        if (d) {
            window.setTimeout(function () {
                Message.check(true)
            }, this.options.interval)
        }
    },
    add: function (d, c, b) {
        if (!c) {
            c = d.substr(0, 20);
            if (d.length > 20) {
                c += "..."
            }
        }
        if (!b) {
            b = {
                style: "",
                callback: false,
                autohide: false
            }
        } else {
            if (typeof b == "string") {
                b = {
                    style: b,
                    callback: false,
                    autohide: false
                }
            } else {
                if (typeof b == "function") {
                    b = {
                        style: "",
                        callback: b,
                        sticky: true,
                        autohide: false
                    }
                } else {
                    if (typeof b == "number") {
                        b = {
                            style: "",
                            callback: false,
                            autohide: b
                        }
                    } else {
                        if (typeof b == "object") {
                            if (b.callback) {
                                b.sticky = true
                            }
                        }
                    }
                }
            }
        }
        var a = new Date().getTime();
        return this.queue.push($.extend(b, {
            time: a,
            unique: a + "_" + (Math.floor(Math.random() * 90000) + 10000),
            text: d,
            title: c,
            unread: true,
            active: false,
            sticky: (b.sticky || b.style.match(/\bsticky\b/i) ? true : false)
        }))
    },
    remove: function (b) {
        for (var a = this.queue.length - 1; a >= 0; a--) {
            if (this.queue[a].unique == b) {
                this.queue.splice(a, 1)
            }
        }
        return true
    },
    get: function (b) {
        for (var a = this.queue.length - 1; a >= 0; a--) {
            if (this.queue[a].unique == b) {
                return this.queue[a]
            }
        }
        return false
    },
    show: function (b) {
        var a = this.get(b);
        $(this.container).append($('<div id="__messageId_' + a.unique + '"></div>').addClass(this.options.messageClass).append($("<div></div>").addClass(this.options.messageWrapperClass).append($("<div></div>").addClass(a.style).append($("<h1></h1>").addClass(this.options.messageTitleClass).html(a.title)).append($("<p></p>").addClass(this.options.messageTextClass).html(a.text))).data("unique", a.unique).click(function () {
            Message.hide($(this).data("unique"))
        }).mouseenter(function () {
            Message.active($(this).data("unique"))
        }).mouseleave(function () {
            Message.inactive($(this).data("unique"))
        })).css({
            opacity: 0
        }).animate({
            opacity: 1
        }, 300));
        $(this.container).hide();
        $(this.container).slideToggle('slow');
    },
    hide: function (b) {
        var a = this.get(b);
        if (a.callback) {
            a.callback()
        }
        this.remove(b);
        $("div#__messageId_" + b).animate({
            height: "0px",
            opacity: "0",
            "margin-bottom": "0px"
        }, 300, "linear", function () {
            $(this).remove()
        })
    },
    active: function (b) {
        var a = this.get(b);
        $("div#__messageId_" + b).addClass(this.options.messageClassActive);
        a.active = true
    },
    inactive: function (b) {
        var a = this.get(b);
        $("div#__messageId_" + b).removeClass(this.options.messageClassActive);
        a.active = false
    }
};

